package com.htgd.gardenexpo.service.impl;

import com.htgd.gardenexpo.entity.GeConIntegralCategory;
import com.htgd.gardenexpo.dao.GeConIntegralCategoryDao;
import com.htgd.gardenexpo.service.GeConIntegralCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.IntegralCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 积分类目 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-31
 */
@Service
public class GeConIntegralCategoryServiceImpl extends ServiceImpl<GeConIntegralCategoryDao, GeConIntegralCategory> implements GeConIntegralCategoryService {

    @Autowired
    private GeConIntegralCategoryDao geConIntegralCategoryDao;

    @Override
    public void delete(int id) {
        geConIntegralCategoryDao.deleteById(id);
        deleteAll(id);

    }

    @Override
    public IntegralCategoryVo querylist(GeConIntegralCategory geConIntegralCategory) {
        IntegralCategoryVo integralCategoryVo = new IntegralCategoryVo();

        List<GeConIntegralCategory> geConIntegralCategories = new ArrayList<>();
        //根目录
        List<GeConIntegralCategory> list = lambdaQuery().list();

        for (GeConIntegralCategory conIntegralCategory : list) {
            if(0==conIntegralCategory.getParentId()){
                geConIntegralCategories.add(conIntegralCategory);
            }
            conIntegralCategory.setKey(String.valueOf(conIntegralCategory.getId()));
            conIntegralCategory.setTitle(conIntegralCategory.getName());
        }
        //下一级
        getListTree(geConIntegralCategories,list);

        integralCategoryVo.setTotal(String.valueOf(list.size()));
        integralCategoryVo.setList(geConIntegralCategories);
        return integralCategoryVo;
    }

    @Override
    public String getNameById(Integer categoryId) {
        return lambdaQuery().eq(GeConIntegralCategory::getId,categoryId)
                .select(GeConIntegralCategory::getName)
                .one()
                .getName();
    }

    public void getListTree(List<GeConIntegralCategory> geConIntegralCategoryList, List<GeConIntegralCategory> list){
        for (GeConIntegralCategory conIntegralCategory : geConIntegralCategoryList) {
            List<GeConIntegralCategory> geConIntegralCategories1 = new ArrayList<>();
            for (GeConIntegralCategory integralCategory : list) {
                if(conIntegralCategory.getId()==integralCategory.getParentId()){
                    geConIntegralCategories1.add(integralCategory);
                }
            }
            conIntegralCategory.setChildren(geConIntegralCategories1);
            //递归
            if(geConIntegralCategories1!=null&&geConIntegralCategories1.size()>0){
                getListTree(geConIntegralCategories1,list);
            }
        }
    }

    public void deleteAll(int id){
        List<GeConIntegralCategory> list = lambdaQuery().eq(GeConIntegralCategory::getParentId, id).list();
        if(list != null && list.size()>0){
            for (GeConIntegralCategory geConIntegralCategory : list) {
                geConIntegralCategoryDao.deleteById(geConIntegralCategory.getId());
                deleteAll(geConIntegralCategory.getId());
            }
        }
    }
}
